﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void BracketToExpander()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is Expressions.Bracket == false)
            {
                MessageBox.Show("此命令仅用于将一个括弧式转换成一个扩展式！请先选中一个括弧式。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;//目前只有括弧式和扩展式支持互转（2010年12月30日）。
            }

            try
            {
                Expressions.Bracket bracket = Globals.selectedEditor.SelectedExpression as Expressions.Bracket;

                StackPanel sp = bracket.ParentPanel.BasePanel;
                int bracketIndex = sp.Children.IndexOf(bracket);

                Expressions.Expander newExpander = new Expressions.Expander(
                    bracket.RootPanel, bracket.ParentPanel, bracket.Editor);

                sp.Children.Insert(bracketIndex, newExpander);


                ModifingItem mi = new ModifingItem() { OldSelectedExID = bracket.ExID };

                string bracketOldXml = bracket.XmlData.OuterXml;

                bracket.XmlData.SetAttribute("Type", "Expander");
                bracket.XmlData.SetAttribute("ExID", Guid.NewGuid().ToString());
                newExpander.XmlData = bracket.XmlData;

                Action actInsert = new Action(newExpander.ExID, bracketIndex,
                        newExpander.GetType().Name, "InsertExpression", null, newExpander.XmlData.OuterXml,
                        ActionType.Insert, newExpander.ParentPanel.ExID);
                mi.Add(actInsert);

                Action actReplaceOutXml = new Action(newExpander.ExID,
                    -1, newExpander.GetType().Name, "OutXmlText", newExpander.XmlData.OuterXml,
                    bracket.XmlData.OuterXml);
                mi.Add(actReplaceOutXml);

                Action actDelete = new Action(bracket.ExID,
                    sp.Children.IndexOf(bracket), bracket.GetType().Name,
                    "DeleteExpression", bracketOldXml, null,
                    ActionType.Delete, bracket.ParentPanel.ExID);
                mi.Add(actDelete);

                sp.Children.Remove(bracket);

                mi.NewSelectedExID = newExpander.ExID;

                newExpander.Editor.Do(mi);
                newExpander.Editor.SelectedExpression = newExpander;
                newExpander.Editor.IsModified = true;
            }
            catch (Exception ex)
            {
                Dialogs.LunarMsgBox.MsgBoxShow(
                    Globals.appName,
                    "在括弧式与扩展式间的转换出现问题。",
                    "错误信息如下：\r\n" + ex.Message, ex.StackTrace,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
        }

        public static void ExpanderToBracket()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is Expressions.Expander == false)
            {
                MessageBox.Show("此命令仅用于将一个扩展式转换成一个括弧式！请先选中一个扩展式。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;//目前只有括弧式和扩展式支持互转（2010年12月30日）。
            }

            try
            {
                Expressions.Expander expander = Globals.selectedEditor.SelectedExpression as Expressions.Expander;

                StackPanel sp = expander.ParentPanel.BasePanel;
                int expanderIndex = sp.Children.IndexOf(expander);

                Expressions.Bracket newBrakcet = new Expressions.Bracket(
                    expander.RootPanel, expander.ParentPanel, expander.Editor);

                sp.Children.Insert(expanderIndex, newBrakcet);


                ModifingItem mi = new ModifingItem()
                {
                    OldSelectedExID = expander.ExID,
                };

                string expanderOldXml = expander.XmlData.OuterXml;

                expander.XmlData.SetAttribute("Type", "Bracket");
                expander.XmlData.SetAttribute("ExID", Guid.NewGuid().ToString());
                newBrakcet.XmlData = expander.XmlData;

                Action actInsert = new Action(newBrakcet.ExID, expanderIndex,
                        newBrakcet.GetType().Name, "InsertExpression", null, newBrakcet.XmlData.OuterXml,
                        ActionType.Insert, newBrakcet.ParentPanel.ExID);
                mi.Add(actInsert);

                Action actReplaceOutXml = new Action(newBrakcet.ExID,
                    -1, newBrakcet.GetType().Name, "OutXmlText", newBrakcet.XmlData.OuterXml,
                    expander.XmlData.OuterXml);
                mi.Add(actReplaceOutXml);

                Action actDelete = new Action(expander.ExID,
                    sp.Children.IndexOf(expander), expander.GetType().Name,
                    "DeleteExpression", expanderOldXml, null,
                    ActionType.Delete, expander.ParentPanel.ExID);
                mi.Add(actDelete);

                mi.NewSelectedExID = newBrakcet.ExID;

                sp.Children.Remove(expander);

                newBrakcet.Editor.Do(mi);
                newBrakcet.Editor.SelectedExpression = newBrakcet;
                newBrakcet.Editor.IsModified = true;
            }
            catch (Exception ex)
            {
                Dialogs.LunarMsgBox.MsgBoxShow(
                    Globals.appName,
                    "在扩展式与括弧式间的转换出现问题。",
                    "错误信息如下：\r\n" + ex.Message,
                    ex.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
        }
    }
}
